package com.poe.controller.filter;

import com.util.DomainUrlUtil;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Set;

/**
 * Created by Mr.欧 on 2017/7/3.
 */
public class AccessFilter implements Filter {
    private Set<String> excludeUrls;
    public void init(FilterConfig config) throws ServletException {
        System.out.println(config);
//        String excludeUrlsStr = config.getInitParameter("excludeUrls");
//        if (null != excludeUrlsStr && !excludeUrlsStr.isEmpty()) {
//            String[] urls = excludeUrlsStr.split(",");
//            this.excludeUrls = new HashSet<String>();
//            for (String url : urls) {
//                this.excludeUrls.add(url);
//            }
//        }
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        // 请求的URI
        String uri = request.getRequestURI();
        String host = request.getRemoteHost();
        HttpSession session;

        try {
            // 放行例外连接地址
//            if (null != this.excludeUrls && !this.excludeUrls.isEmpty()) {
//                String tempURI = -1 != uri.indexOf("?") ? uri.substring(0, uri.indexOf("?")) : uri;
//                if (this.excludeUrls.contains(tempURI)) {
//                    filterChain.doFilter(request, response);
//                    return;
//                }
//            }
            if(request.getSession(false) == null){
                session= request.getSession(true);
                session.setAttribute("domainUrl", DomainUrlUtil.getEJS_URL_RESOURCES());
                session.setAttribute("domainStaticUrl", DomainUrlUtil.getEJS_STATIC_RESOURCES());
            }
            filterChain.doFilter(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void destroy() {

    }
}
